﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.Data.Entity;


namespace SharpErp.Data.Common.Context
{
    public class SqliteDbContext : DbContext
    {
        public DbSet<Entity.ItemClass> ItemClasses{ get; set; }
        
        public DbSet<Entity.KVEntity> KVEntities{get;set;}
        
        public DbSet<Entity.CustomerSecondClass> CustomerSencodClasEntities{get;set;}
        
        public DbSet<Entity.CustomerThirdClass> CustomerThirdClasEntities{get;set;}

        public DbSet<Entity.ItemSevenClass> ItemSevenClasses { get; set; }

        public DbSet<Entity.ItemEightClass> ItemEightClasses { get; set; }

        public DbSet<Entity.ItemNineClass> ItemNineClasses { get; set; }
        
        public DbSet<Entity.MaterialSevenClass> MaterialSevenClasses { get; set; }

        public DbSet<Entity.MaterialEightClass> MaterialEightClasses { get; set; }

        public DbSet<Entity.MaterialNineClass> MaterialNineClasses { get; set; }
        
        public DbSet<Entity.GoodsMaterialClass> GoodsMaterialClasses { get; set; }

        public DbSet<Entity.CustomerGoodsClass> CustomerGoodsClasses { get; set; }
        
		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			optionsBuilder.UseSqlite( "Filename=" + System.Environment.CurrentDirectory + "\\test.db3");
			base.OnConfiguring(optionsBuilder);
		}
		
		protected override void OnModelCreating(ModelBuilder modelBuilder)
		{
			base.OnModelCreating(modelBuilder);
		}
		
		public IQueryable<T> All<T>(DbSet<T> dbSet) where T : Common.Entity.BaseEntity
		{
			return dbSet.Where(p=>(p.SystemFlag & Common.Entity.BaseEntity.SYS_FLAG_DELETE) == 0);
		}
		
		public T[] AllArray<T>(DbSet<T> dbSet) where T : Common.Entity.BaseEntity
		{
			return dbSet.Where(p=>(p.SystemFlag &  Common.Entity.BaseEntity.SYS_FLAG_DELETE) == 0).ToArray<T>();
		}
		
		public T SelectOne<T>(DbSet<T> dbSet, Expression<Func<T, bool>> predicate) where T : Common.Entity.BaseEntity
		{
			return All<T>(dbSet).FirstOrDefault(predicate);
		}
		
		public T[] SelectAll<T>(DbSet<T> dbSet, Expression<Func<T, bool>> predicate) where T : Common.Entity.BaseEntity
		{
			return All<T>(dbSet).Where(predicate).ToArray<T>();
		}
		
		public void Delete<T>(DbSet<T> dbSet, Expression<Func<T, bool>> predicate) where T : Common.Entity.BaseEntity
		{
			var array = All<T>(dbSet).Where(predicate).ToArray<T>();
			foreach(var item in array)
			{
				item.SystemFlag = item.SystemFlag | Common.Entity.BaseEntity.SYS_FLAG_DELETE;
			}
			this.SaveChanges();
		}
		
		public T Add<T>(DbSet<T> dbSet, T data) where T : Common.Entity.BaseEntity
		{
		    data.CreateTime = data.LastModifyTime = DateTime.Now;
			var result = dbSet.Add(data);
			this.SaveChanges();
            return result.Entity;
		}
		
		public void Update<T>(DbSet<T> dbSet, T data) where T : Common.Entity.BaseEntity
		{
            data.LastModifyTime = DateTime.Now;
			dbSet.Update(data);
			this.SaveChanges();
		}
		
		public void Clear<T>(DbSet<T> dbSet, Expression<Func<T, bool>> predicate) where T : Common.Entity.BaseEntity
		{
			var array = dbSet.Where(predicate).ToArray<T>();
			dbSet.RemoveRange(array);
			this.SaveChanges();
		}
	}
}

